var server = require('../../utils/server');
var common = require('../../utils/common');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    list: [],
    isEmpty: false,
    isFirstSearch: true,
    searchLoading: false, //"上拉加载"的变量，默认false，隐藏
    searchLoadingComplete: true ,//“没有数据”的变量，默认false，隐藏
    isvip:0
  },
  onLoad(options) {
    console.log(options)
    this.data.isvip=options.isvip==undefined?0:options.isvip;
  },
  onShow() {
    this.searchByStatus();
  },
  searchByStatus() {
    common.showLoading();
    common.hideLoading();
    this.setData({
      searchLoadingComplete: true, //把“没有数据”设为true，显示
      searchLoading: false //把"上拉加载"的变量设为false，隐藏
    })
    console.log(this.data.isvip)
    server.postJSON(common.storePath + 'coupon/couponloglist/', {
      "page": 1,
      "pageSize": 10,
      "status": 3,
      "is_vip":this.data.isvip
    }, res => {
      if (res.data.errno == '0') {
        this.setData({
          list: res.data.data,
          isEmpty: res.data.data.length > 0 ? false : true,
          searchLoadingComplete: res.data.data.length < 10 ? true : false,
          searchLoading: res.data.data.length < 10 ? false : true
        })
      } else {
        common.showErrorToast(res.data.errmsg);
      }
    })
  },
  //滚动到底部触发事件  
  onReachBottom() {
    console.log(this.data.page)
    let that = this;
    if (that.data.searchLoading && !that.data.searchLoadingComplete) {
      // if (that.data.list.length > 4) {
      common.showLoading();
      common.hideLoading();
      that.setData({
        page: that.data.page + 1 //每次触发上拉事件，把searchPageNum+1 
      });
      that.getMoreData()
      // }
    }

  },
  getMoreData() {
    server.postJSON(common.storePath + 'coupon/couponloglist/', {
      "page": this.data.page,
      "pageSize": 10,
      "status": 3,
      "is_vip":this.data.isvip
    }, res => {
      console.log(res)
      if (res.data.errno == "0") {
        common.hideLoading();
        if (res.data.data.length > 0) {
          console.log(res.data.data)
          var newData = this.data.list;
          newData = newData.concat(res.data.data)
          this.setData({
            list: newData,
            isEmpty: res.data.data.length > 0 ? false : true,
            searchLoadingComplete: res.data.data.length < 10 ? true : false,
            searchLoading: res.data.data.length < 10 ? false : true,
          })
        } else {
          this.setData({
            searchLoadingComplete: true, //把“没有数据”设为true，显示
            searchLoading: false //把"上拉加载"的变量设为false，隐藏
          });
        }
      } else {

        common.showErrorToast(res.data.errmsg);
      }
    })
  },
  goToCredit() {
    wx.navigateTo({
      url: 'creditShop',
    })
  }
})